package com.fly.core;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

import com.fly.core.ds.BaseDataSource;
import com.fly.core.ds.SpringPool;

/**
 * 
 * 数据源连接、事务支持管理工具
 * 
 * @author 00fly
 * @version [版本号, 2018-09-26]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MySqlDBUtil
{
    // 此处切换数据源
    private static BaseDataSource dataSource = SpringPool.getInstance();
    
    /**
     * <默认构造函数>
     */
    private MySqlDBUtil()
    {
        super();
    }
    
    /**
     * 获取数据库连接
     * 
     * @return
     * @throws SQLException
     * @throws ExecutionException
     * @throws InterruptedException
     * @see [类、类#方法、类#成员]
     */
    public static Connection getConnection()
        throws SQLException
    {
        return dataSource.getConnection();
    }
    
    /**
     * 释放数据库连接
     * 
     * @see [类、类#方法、类#成员]
     */
    public static void close(Connection connection)
    {
        dataSource.close(connection);
    }
    
    /**
     * 开启事务
     * 
     * @throws SQLException
     */
    public static void startTransaction()
        throws SQLException
    {
        dataSource.startTransaction();
    }
    
    /**
     * 提交事务并关闭连接
     * 
     * @throws SQLException
     * 
     */
    public static void commitAndClose()
        throws SQLException
    {
        dataSource.commitAndClose();
    }
    
    /**
     * 回滚事务并关闭连接
     * 
     * @throws SQLException
     * 
     */
    public static void rollbackAndClose()
        throws SQLException
    {
        dataSource.rollbackAndClose();
    }
}
